/**
 * NearShop
 * FindShopAction.java
 * com.android.nearshop.io.protocol.in
 * 
 */
package com.android.nearshop.io.protocol.in;

import java.util.ArrayList;
import java.util.List;

import android.util.Log;

import com.android.nearshop.io.protocol.out.ShopList;

/*********************************************************
 * @author : zhaohua
 * @version : 2012-8-11
 * @see : 根据参数，查询商家列表。商家搜索与列表。
 * @Copyright : copyrights reserved by personal 2007-2012
 **********************************************************/
public class FindShopAction extends SoapAction<ShopList>
{
    public int curPage;
    public int pageNum;

	/**
     * 查询商家列表。以下是必填参数
     * @param lon 精度
     * @param lat 纬度
     * @param isShake 是否是摇动，否则就是查询
     * @param curPage 第几页的页码，[1,)
     * @param pageNum 每页的数量
     */
    public FindShopAction(double lon, double lat, boolean isShake, int curPage, int pageNum)
    {
        super(ACTION_TYPE.ACTION_SHOP,"findShopList");
        addJsonParam("position", lon+","+lat);
        addJsonParam("requestUrl",isShake ? "shake": "query");
        setPage(curPage, pageNum);
    }
    
    /** 设置分页信息*/
    public void setPage(int curPage, int pageNum)
    {
        addJsonParam("pageStruct",curPage+","+pageNum);
        this.pageNum = pageNum;
        this.curPage = curPage;
    }
    
    /** 设置坐标*/
    public void setLocation(double lon, double lat)
    {
    	addJsonParam("position", lon+","+lat);
    }
    
    /**
     * 行业Id，多个以逗号隔开。
     * @param ids
     */
    public void setCatalogyIds(List<Integer> ids)
    {
        if(ids != null && ids.size() > 0)
        {
            StringBuilder buf = new StringBuilder();
            for(int i = 0; i < ids.size() ; i++)
            {
                if(i != 0)
                {
                    buf.append(",");
                }
                buf.append(ids.get(i));
            }
            addJsonParam("catalogyIds", buf.toString());
        }else
        {
            addJsonParam("catalogyIds", null);
        }
    }
    
    /**
     * 返回协议中的分类id列表，没有则返回null
     * @return
     */
    public List<Integer> getCatalogyIds()
    {
        String cateids = (String) getJsonParam("catalogyIds");
        if(cateids == null)
        {
            return null;
        }else
        {
            String[] cateArray = cateids.split(",");
            List<Integer> list = new ArrayList<Integer>(cateArray.length);
            for(String cate : cateArray)
            {
                try {
                    list.add(new Integer(cate));
                }catch(Exception e)
                {
                   e.printStackTrace(); 
                }
            }
            return list;
        }
    }
    
    /**
     * 关键字
     * @param keyword
     */
    public void setKeyword(String keyword)
    {
        addJsonParam("keyword",keyword);
    }
    
    /**
     * 镇区id
     * @param areaId
     */
    public void setAreaId(int areaId)
    {
        addJsonParam("areaId",areaId > 0 ? areaId +"" : null);
    }
    
    /**
     * 获取镇区id，没有则返回0
     * @return
     */
    public int getAreaId()
    {
        String areaId = (String) getJsonParam("areaId");
        if(areaId == null)
        {
            return 0;
        }else
        {
            try {
                new Integer(areaId);
            }catch(NumberFormatException e)
            {
                e.printStackTrace();
            }
            return 0;
        }
    }
    
    /**
     * 距离搜索，单位(米)
     * @param radius
     */
    public void setRadius(int radius)
    {
        addJsonParam("radius",radius > 0 ? radius +"" : null);
    }
    
    /**
     * 排序方式有三种：距离、星级、访问量；
     * @param distance 是否按距离
     * @param distanceDesc true：降序，否则升序
     * @param recommend 是否按星级
     * @param recommendDesc true：降序，否则升序
     * @param clickrates 是否按访问量
     * @param clickratesDesc true：降序，否则升序
     */
    public void setOrderStruct(
            boolean distance, boolean distanceDesc,
            boolean recommend , boolean recommendDesc,
            boolean clickrates , boolean clickratesDesc)
    {
        ProtocolHelper.setOrderStruct(this, distance, distanceDesc, recommend, recommendDesc, clickrates, clickratesDesc);
    }
    
    /* (non-Javadoc)
     * @see com.android.nearshop.io.protocol.in.SoapAction#parseJson(java.lang.String)
     */
    @Override
    protected ShopList parseJson(String response) throws Exception
    {
        Log.d(getMethod(), getMethod() + "response: \n " + response);
        ShopList res = new ShopList();
        res.parseData(response);
        return res;
    }

}
